<template>
  <div class="oe-approval-paper" v-loading="loading">
    <a-tooltip>
      <template slot="title">回到顶部</template>
      <a-back-top />
    </a-tooltip>
    <info-top :showExpend="false"></info-top>
    <div class="oe-ap-paper-container">
      <top-tip v-if="examTitle" :examTitle="examTitle"></top-tip>
      <div class="oe-ap-paper-content" v-if="paperList.length">
        <div class="oe-ap-left">
          <div class="oe-stu-mess clearfix">
            <img :src="stuAvator" class="oe-stu-img">
            <div class="oe-stu-message">
              <span>姓名:{{displayName}}</span>
              <div>用户名:{{userName}}</div>
            </div>
          </div>
          <answer-card
            :questionList="questionList"
            :answerCardTop="190"
            type="right"
            @getBtnAction="getBtnAction"
            :showNext="showNext"
          ></answer-card>
        </div>
        <div class="oe-ap-center" style="width: calc(100% - 334px);margin-right: 0px;" v-if="paperShow">
          <teaTestPaper :questionData="paperList" :showAnswer="true" :numInputStatus='"review"'></teaTestPaper>
        </div>
      </div>
      <div class="paper-no-data" v-if="!paperList.length">
        <blank-tips></blank-tips>
      </div>
    </div>
  </div>
</template>

<script>
import infoTop from '@/components/info-top/index.vue';
import topTip from '@/components/top-tip/index.vue';
import answerCard from '@/components/answer-card/index.vue';
import blankTips from '@/components/blank-tips/index.vue';
import teaTestPaper from '@/components/teaTestPaper/index.vue';
import GetOperationList from '@/lib/mixin/methods.js';
export default {
  name: 'approvalPaper',
  mixins: [GetOperationList],
  components: {
    infoTop,
    topTip,
    answerCard,
    blankTips,
    teaTestPaper
  },
  data() {
    return {
      // 左边控制
      questionList: [],
      paperList:[],
      loading: false,
      isShowScore: true,
      // 显示批阅下一个
      showNext: false,
      // 批阅卷子的id
      workExamStuId: '',
      displayName: '',
      userName: '',
      stuAvator: '',
      paperShow: true,
      modeType: 'review',
      examTitle:''
    };
  },
  props:{
    examid:{
      type:String,
      default:''
    }
  },
  mounted(){
    this.getReviewPaper();
    this.getOpeartion()
  },
  methods: {
    getOpeartion() {
      this.loading = true;
      this.GetOperationList('examId',this.examid).then((rst) => {
        this.loading = false;
        if (rst.code === 1) {
          this.examTitle = rst.examTitle
        } else {
          this.$message.error(rst.msg);
        }
      });
    },
    // 获取批阅娟
    getReviewPaper(type) {
      this.loading = true;
      this.$http
        .post('/api/design/marking/getMarkTeaQue', {
          examId: this.examid
        })
        .then((rst) => {
          this.loading = false;
          if (rst.code > 0) {
            this.showNext = false;
            // 初试化题目列表
            this.paperList = rst.list
            this.workExamStuId = rst.workExamStuId;
            this.displayName = rst.displayName;
            this.userName = rst.userName;
            this.stuAvator = rst.stuAvator;
            if (rst.list.length) {
              this.paperShow = false;
              // 初始化左边卡片
              this.questionList = rst.list;
              this.$forceUpdate();
              setTimeout(() => {
                this.paperShow = true;
              }, 200);
            }
          } else {
            this.$message.error(rst.msg);
            this.$router.back();
          }
        });
    },
    // 批改单题
    changeSingleQues(val,type) {
      for (let i = 0; i < this.questionList.length; i++) {
        if (this.questionList[i].QuestionId == val.QuestionId) {
          this.questionList[i].IsAssignmented = true;
        }
      }
      let bigTotalScore = val.Score,otherSubScore=null;
      if(type === 1){
        this.paperList.forEach(item => {
          item.SubQuesList && item.SubQuesList.forEach(item2=>{
            if(item2.Id === val.Id){
              bigTotalScore = item.Score
              otherSubScore = item2.otherSubScore
            }
          })
        })
      }
      this.$http
        .post('/api/design/marking/markOnlineExam', {
          getScore: type === 0 ? val.GetScore : val.StuDo.GetScore,
          studentWorkId: type === 0 ? val.StuAnswerId : val.StuDo.StuSubAnswerId,
          type, // 大题0  小题1
          bigTotalScore,
          otherSubScore,
        })
        .then((rst) => {
          if (rst.code > 0) {
            if(type === 0){ //大题
              let index = this.paperList.findIndex((qes) => {
                return qes.QuestionId === val.QuestionId;
              });
              if (index > -1) {
                // 如果正确
                if (val.GetScore === val.Score) {
                  this.paperList[index].IsRight = 1;
                }
                // 如果错误
                if (!val.GetScore) {
                  this.paperList[index].IsRight = 2;
                }
                // 半对
                if (val.GetScore && val.GetScore < val.Score) {
                  this.paperList[index].IsRight = 3;
                }
              }
            }else{   //小题
              this.paperList.forEach(item=>{ 
                if(item.SubQuesList){ //是否存在小题
                  item.SubQuesList.forEach(subItem=>{
                    if(subItem.Id === val.Id){ //批阅id相同的小题，拿到父级项
                      let sum = 0
                      item.SubQuesList.forEach(subItem2=>{ //算总得分
                        sum = sum + subItem2.StuDo.GetScore
                      })
                      sum = sum + subItem.otherSubScore  //加上其余客观题
                      if (sum === item.Score) {
                        item.IsRight = 1;
                      }else if (sum === 0) {  // 如果错误
                        item.IsRight = 2;
                      }else if (sum < item.Score && sum > 0) {// 半对
                        item.IsRight = 3;
                      }
                    }
                  })
                }
              })
            }
            
            this.questionList = this.paperList;
          } else {
            this.$message.error(rst.msg);
          }
        });
    },
    // 按钮的操作
    getBtnAction(val) {
      switch (val) {
        case 'next':
          this.getReviewPaper('next');
          this.modeType = 'review';
          break;
        case 'submit':
          this.saveApproval();
          break;
        default:
          break;
      }
    },
    // 保存
    saveApproval() {
      this.loading = true;
      this.$http
        .post('/api/design/marking/saveTeaQueTypeMark', {
          workExamId: this.examid,
          workExamStuId: this.workExamStuId
        })
        .then((rst) => {
          this.loading = false;
          if (rst.code > 0) {
            this.showNext = true;
            this.$message.success('提交批阅成功');
            this.modeType = 'result';
          } else {
            this.$message.error(rst.msg);
          }
        });
    }
  }
};
</script>
